﻿(function(jQuery) {
    jQuery.fn.checkDate = function(options) {
        var defaults = {
            Date: '',
            Message: '',
            Controls: []
        };
        var options = jQuery.extend({}, defaults, options);
        var days = 0;
        var blnDate = true;
        this.each(function() {
            //$(this).val(options.Message);
            // Verifica fecha ingresada
            blnDate = checkDate($(this));
            // Compara fechas
            if (options.Date != '' && blnDate == true) {
                days = DaysDiff($(this).val(), options.Date);
                if (days > 0) {
                    blnDate = false;
                    $(this).addClass('ui-state-error');
                    for (i = 0; i < options.Controls.length; i++) {
                        options.Controls[i].addClass('ui-state-error');
                    };
                    if (options.Message == '') { options.Message = 'La fecha ingresada no puede ser mayor a la actual.'; };
                    alert(options.Message);
                    $(this).focus();
                } else {
                    blnDate = true;
                    $(this).removeClass('ui-state-error');
                    for (i = 0; i < options.Controls.length; i++) {
                        options.Controls[i].removeClass('ui-state-error');
                    };
                }
            };
        });
        return blnDate;
    };

    jQuery.fn.checkSelect = function(options) {
        var defaults = {
            Value: '',
            Message: '',
            Data: []
        };
        var options = jQuery.extend({}, defaults, options);
        var blnSelect = true;
        this.each(function() {
            if (options.Value != '') {
                if ($(this).val() != options.Value) {
                    $(this).addClass('ui-state-error');
                    alert(options.Message);
                    $(this).focus();
                    blnSelect = false;
                } else {
                    $(this).removeClass('ui-state-error');
                    blnSelect = true;
                };
            } else {
                if ($(this).val() == '' || $(this).val() == '0') {
                    $(this).addClass('ui-state-error');
                    alert(options.Message);
                    $(this).focus();
                    blnSelect = false;
                } else {
                    $(this).removeClass('ui-state-error');
                    blnSelect = true;
                };
            };
        });
        return blnSelect;
    };

    jQuery.fn.checkText = function(options) {
        var defaults = {
            Value: '',
            Message: '',
            Data: [],
            MinLength: null,
            MaxLength: null,
            Operator: 'And',
            Control: null
        };
        var options = jQuery.extend({}, defaults, options);
        var blnText = true;
        this.each(function() {
            blnText = jQuery.trim($(this).val()) == options.Value ? false : true;
            for (i = 0; i < options.Data.length; i++) {
                if (options.Operator == 'And') {
                    blnText = (blnText) && (jQuery.trim(options.Data[i].val()) == options.Value ? false : true);
                } else {
                    blnText = (blnText) || (jQuery.trim(options.Data[i].val()) == options.Value ? false : true);
                };
            };
            if (!blnText) {
                (options.Control == null) ? $(this).addClass('ui-state-error') : options.Control.addClass('ui-state-error');
                for (i = 0; i < options.Data.length; i++) {
                    options.Data[i].addClass('ui-state-error');
                };
                if (options.Message != '') {
                    alert(options.Message);
                };
                (options.Control == null) ? $(this).focus() : options.Control.focus();
            } else {
                (options.Control == null) ? $(this).removeClass('ui-state-error') : options.Control.removeClass('ui-state-error');
                for (i = 0; i < options.Data.length; i++) {
                    options.Data[i].removeClass('ui-state-error');
                };
            };
        });
        return blnText;
    };

    jQuery.fn.Compare = function(options) {
        var defaults = {
            Value: '',
            Message: '',
            Data: []
        };
        var options = jQuery.extend({}, defaults, options);
        var blnCompare = true;
        this.each(function() {
            blnCompare = options.Data.length <= 0 ? false : true;
            for (i = 0; i < options.Data.length; i++) {
                blnCompare = (blnCompare) && (jQuery.trim($(this).val()) == jQuery.trim(options.Data[i].val()) ? true : false);
            };
            if (!blnCompare) {
                $(this).addClass('ui-state-error');
                for (i = 0; i < options.Data.length; i++) {
                    options.Data[i].addClass('ui-state-error');
                };
                if (options.Message != '') {
                    alert(options.Message);
                };
                $(this).focus();
            } else {
                $(this).removeClass('ui-state-error');
                for (i = 0; i < options.Data.length; i++) {
                    options.Data[i].removeClass('ui-state-error');
                };
            };
        });
        return blnCompare;
    };

    jQuery.fn.ReadOnly = function(options) {
        var defaults = {
            Value: true
        };
        var options = jQuery.extend({}, defaults, options);
        this.each(function() {
            var type = $(this).attr("type");
            if (options.Value) {
                $(this).attr('disabled', 'disabled');
                $(this).addClass('Deshabilitado');
            } else {
                $(this).attr('disabled', '');
                $(this).removeClass('Deshabilitado');
            };
        });
    };

    jQuery.fn.CheckEmail = function(options) {
        var defaults = {
            Requiered: false,
            Message: ''
        };
        var options = jQuery.extend({}, defaults, options);
        var blnEmail = true;
        this.each(function() {
            if (options.Requiered) {
                blnEmail = (jQuery.trim($(this).val()) != '') ? true : false;
            };
            if (jQuery.trim($(this).val()) != '') {
                if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(jQuery.trim($(this).val()))) {
                    blnEmail = true;
                }
                else {
                    blnEmail = false;
                };
            };
            if (!blnEmail) {
                $(this).addClass('ui-state-error');
                if (options.Message != '') { alert(options.Message); };
                $(this).focus();
            } else {
                $(this).removeClass('ui-state-error');
            };
        });
        return blnEmail;
    };

    jQuery.fn.checkVal = function(options) {
        var defaults = {
            Message: '',
            MinValue: null,
            MaxValue: null
        };
        var options = jQuery.extend({}, defaults, options);
        var blnVal = true;
        var value = 0;
        this.each(function() {
            value = Number($(this).val());
            blnVal = (value >= options.MinValue) && (value <= options.MaxValue);
            if (!blnVal) {
                $(this).addClass('ui-state-error');
                if (options.Message != '') { alert(options.Message); };
                $(this).focus();
            } else {
                $(this).removeClass('ui-state-error');
            };
        });
        return blnVal;
    };

    jQuery.fn.ControlsDId = function(options) {
        var defaults = {
            Controls: []
        };
        var options = jQuery.extend({}, defaults, options);
        var blnControls = true;

        this.each(function() {
            if (options.Controls.length >= 4) {
                options.Controls[0].ReadOnly({ Value: false });
                options.Controls[1].ReadOnly({ Value: false });
                options.Controls[2].ReadOnly({ Value: false });
                options.Controls[3].ReadOnly({ Value: false });
                options.Controls[4].ReadOnly({ Value: false });
                options.Controls[0].val('');
                options.Controls[1].val('');
                options.Controls[2].val('');
                options.Controls[3].val('');
                options.Controls[4].val('');
                if ($(this).val() == 0) {
                    options.Controls[0].ReadOnly();
                    options.Controls[1].ReadOnly({ Value: false });
                    options.Controls[2].ReadOnly({ Value: false });
                    options.Controls[3].ReadOnly({ Value: false });
                    options.Controls[4].ReadOnly();
                } else if ($(this).val() == 1) {
                    options.Controls[0].ReadOnly({ Value: false });
                    options.Controls[1].ReadOnly();
                    options.Controls[2].ReadOnly();
                    options.Controls[3].ReadOnly();
                    options.Controls[4].ReadOnly({ Value: false });
                } else if ($(this).val() > 1) {
                    options.Controls[0].ReadOnly({ Value: false });
                    options.Controls[1].ReadOnly({ Value: false });
                    options.Controls[2].ReadOnly({ Value: false });
                    options.Controls[3].ReadOnly({ Value: false });
                    options.Controls[4].ReadOnly();
                }
            }
        });
        return blnControls;
    };

    jQuery.HtmlToString = function(Text) {
        var regexp = /(<([^>]+)>)/ig;
        var strHtml = '';
        strHtml = Text.replace(regexp, '');
        strHtml = strHtml.replace('&nbsp;', '');
        return strHtml;
    }

    jQuery.GetHtmlEditor = function(Obj) {
        return $find(Obj);
    }

    jQuery.ChildNodes = function(Obj, item, value) {
        var result;
        if (Obj.cells[item].childNodes.length > 0) {
            result = Obj.cells[item].childNodes.item(0).nodeValue;
        } else {
            result = value;
        }
        return result;
    }

    function DaysDiff(first, second) {
        var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
        var firstDate = parseDate(first);
        var secondDate = parseDate(second);

        var diffDays = ((firstDate.getTime() - secondDate.getTime()) / (oneDay));
        return diffDays;
    };

    function parseDate(str) {
        var date = str.split('/');
        return new Date(date[2], (date[1] - 1), date[0]);
    };

    function checkDate(obj) {
        var strFecha = obj.val();
        if (strFecha != undefined && strFecha.value != "") {
            if (!/^\d{2}\/\d{2}\/\d{4}$/.test(strFecha)) {
                obj.addClass('ui-state-error');
                alert("El formato de fecha no es válido, ingrese con el siguiente formato (dd/mm/aaaa).");
                obj.focus();
                return false;
            }
            var dia = parseInt(strFecha.substring(0, 2), 10);
            var mes = parseInt(strFecha.substring(3, 5), 10);
            var anio = parseInt(strFecha.substring(6), 10);
            switch (mes) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    numDias = 31;
                    break;
                case 4: case 6: case 9: case 11:
                    numDias = 30;
                    break;
                case 2:
                    if (checkBisisesto(anio)) { numDias = 29 } else { numDias = 28 };
                    break;
                default:
                    obj.addClass('ui-state-error');
                    alert("La fecha ingresada es erróonea.");
                    obj.focus();
                    return false;
            }
            if (dia > numDias || dia == 0) {
                obj.addClass('ui-state-error');
                alert("La fecha ingresada es errónea.");
                obj.focus();
                return false;
            }
            obj.removeClass('ui-state-error');
            return true;
        }
    }

    function checkBisisesto(anio) {
        if ((anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
            return true;
        }
        else {
            return false;
        }
    }

})(jQuery);